<template>
  <el-dialog :title="dialog.title" :visible.sync="dialog.show" width="1000px" custom-class="mydialog" append-to-body @close="handleClose">
    <div class="dialog-content">
      <el-table :data="tableData" :summary-method="computeNum" border show-summary style="width: 100%" class="dialog-table" >
        <el-table-column prop="delivery" label="发货方式" width="120">
          <template slot-scope="scope">
            <span v-if="scope.row.delivery===1">供应商代发</span>
            <span v-else-if="scope.row.delivery===2">仓库自发</span>
          </template>
        </el-table-column>
        <el-table-column prop="supplierName" label="供应商" width="120"/>
        <el-table-column prop="skuName" label="商品名称" width="120"/>
        <el-table-column prop="amount" label="商城订单量"/>
        <el-table-column prop="outAmount" label="需出货量">
          <template slot-scope="scope">
            {{ scope.row.amount - scope.row.outAmount }}
          </template>
        </el-table-column>
        <el-table-column prop="stockAmount" label="库存量">
          <template slot-scope="scope">
            {{ scope.row.stockAmount ? scope.row.stockAmount : 0 }}
          </template>
        </el-table-column>
        <el-table-column prop="purchaseNum" label="申请采购量">
          <template slot-scope="scope">
            <el-input v-model=" scope.row.purchaseNum" @input="changeNum(scope.row)"/>
          </template>
        </el-table-column>
        <el-table-column prop="purchasePrice" label="采购单价">
          <template slot-scope="scope">
            <el-input v-model=" scope.row.purchasePrice" @input="changeNum(scope.row)"/>
          </template>
        </el-table-column>
        <el-table-column prop="amount5" label="采购合计" >
          <template slot-scope="scope">
            ￥{{ scope.row.purchasePrice * scope.row.purchaseNum }}
          </template>
        </el-table-column>
      </el-table>
      <div class="text-center mt-30">
        <el-button class="greenbt" type="primary" @click="purchaseFun()">申请采购</el-button>
      </div>
    </div>
  </el-dialog>
</template>
<script>
import { purchaseTransfer } from '@/api/pas/delivery'
export default{
  props: {
    modalStaticData: {
      type: Object,
      default: () => {}
    },
    // 父组件调用时传进来的参数
    modalData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      dialog: {
        title: '', show: true
      },
      tableData: [],
      search: {
        shipmentRecord: ''
      }
    }
  },
  mounted() {
    // 基本信息
    this.dialog.title = '转采购'
    this.tableData = this.modalData
  },
  methods: {
    handleClose() {
      this.$emit('close')
    },

    // 转采购
    purchaseFun() {
      // alert(JSON.stringify(this.tableData));
      for (var i = 0; i < this.tableData.length; i++) {
        if (this.tableData[i].purchaseNum === '0' || this.tableData[i].purchaseNum === null || this.tableData[i].purchaseNum === '') {
          this.$message('采购量不能为0或空！')
          return
        }
        if (isNaN(this.tableData[i].purchaseNum)) {
          this.$message('采购量必须是数字！')
          return
        }
        var xc = this.tableData[i].amount * 1 - this.tableData[i].stockAmount * 1
        if (parseInt(xc) < parseInt(this.tableData[i].purchaseNum)) {
          this.$message('采购量过多！')
          return
        }
      }

      this.search.shipmentRecord = JSON.stringify(this.tableData)
      purchaseTransfer(this.search)
        .then(response => {
          if (response.data.status === 'success') {
            this.$message({
              message: '转采购成功!',
              type: 'success'
            })
            this.handleClose()
          }
        }).finally(() => {
          this.modalStaticData.closeChildAndRefresh()
        })
    },

    changeNum(item) {
      item.amount5 = Number(item.amount3 * item.amount4)
    },
    computeNum(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        }
        const values = data.map(item =>
          column.property === 'amount5' ? Number(item['purchaseNum']) * Number(item['purchasePrice']) : Number(item[column.property])
        )
        // 验证每个value值是否是数字，如果是执行if
        if (index === 1 || index === 2 || index === 4 || index === 7 || index === 6) {
          //
        } else {
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              return prev + curr
            }, 0)
            if (index === 8) {
              var tempVal = Number(sums[index]).toFixed(2)
              sums[index] = '￥' + tempVal
            }
          } else {
            sums[index] = 'NAN'
          }
        }
      })
      return sums
    }
  }

}
</script>
